﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using NAudio.Wave;
using System.IO;

namespace Piirto
{
    public partial class SelectSoundsDialog : Form
    {
        private string soundFolder;
        public string Winsoundpath, Crashsoundpath, Movesoundpath;
        private SoundPlayer winsound;
        private SoundPlayer crashsound;
        private SoundPlayer movesound;
        private WaveIn waveIn;
        private WaveFileWriter writer;
        private bool isRecording;
        private Button recorder;


        public SelectSoundsDialog(string soundFolder)
        {
            InitializeComponent();
            isRecording = false;
            recorder = null;
            this.soundFolder = soundFolder;
            openFileDialog1.Title = "Avaa äänitiedosto";
            this.winsound = new SoundPlayer();
            this.crashsound = new SoundPlayer();
            this.movesound = new SoundPlayer();
        }

        private void btnOpenSound_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                if (sender == btnOpenWinSound)
                {
                    winsound.SoundLocation = openFileDialog1.FileName;
                    Winsoundpath = openFileDialog1.FileName;
                }
                if (sender == btnOpenCrashSound)
                {
                    crashsound.SoundLocation = openFileDialog1.FileName;
                    Crashsoundpath = openFileDialog1.FileName;
                }
                if (sender == btnOpenMoveSound)
                {
                    movesound.SoundLocation = openFileDialog1.FileName;
                    Movesoundpath = openFileDialog1.FileName;
                }
            }
        }

        private void btnListenSound_Click(object sender, EventArgs e)
        {
            if (sender == btnListenWinSound)
            {
                winsound.Play();
            }
            if (sender == btnListenCrashSound)
            {
                crashsound.Play();
            }
            if (sender == btnListenMoveSound)
            {
                movesound.Play();
            }
        }


        private void btnRecordSound_Click(object sender, EventArgs e)
        {
            if (isRecording && recorder==sender)
            {
                waveIn.StopRecording();
                writer.Dispose();
                writer = null;
                waveIn.Dispose();
                recorder.BackgroundImage = global::Piirto.Properties.Resources.start_recording;
                recorder = null;
                if (sender == btnRecordWinSound)
                {
                    winsound.SoundLocation = Winsoundpath;
                }
                if (sender == btnRecordCrashSound)
                {
                    crashsound.SoundLocation = Crashsoundpath;
                }
                if (sender == btnRecordMoveSound)
                {
                    movesound.SoundLocation = Movesoundpath;
                }
                enableAllButtons();
                isRecording = false;
            }
            else if (recorder==null)
            {
                isRecording = true;
                recorder = sender as Button;
                disableAllOtherButtons(recorder);
                StringBuilder audiopath = new StringBuilder();
                audiopath.Append(soundFolder);
                if (audiopath[audiopath.Length - 1] != '/')
                {
                    audiopath.Append('/');
                }
                if (sender == btnRecordWinSound)
                {
                    btnRecordWinSound.BackgroundImage = global::Piirto.Properties.Resources.stop_recording;
                    audiopath.Append("win_");
                    audiopath.Append(DateTime.Now.ToFileTime());
                    audiopath.Append(".wav");
                    Winsoundpath = audiopath.ToString();
                }
                if (sender == btnRecordCrashSound)
                {
                    btnRecordCrashSound.BackgroundImage = global::Piirto.Properties.Resources.stop_recording;
                    audiopath.Append("crash_");
                    audiopath.Append(DateTime.Now.ToFileTime());
                    audiopath.Append(".wav");
                    Crashsoundpath = audiopath.ToString();
                }
                if (sender == btnRecordMoveSound)
                {
                    btnRecordMoveSound.BackgroundImage = global::Piirto.Properties.Resources.stop_recording;
                    audiopath.Append("move_");
                    audiopath.Append(DateTime.Now.ToFileTime());
                    audiopath.Append(".wav");
                    Movesoundpath = audiopath.ToString();
                }
                if (File.Exists(audiopath.ToString()))
                {
                    File.Delete(audiopath.ToString()); //Overwrite
                }
                waveIn = new WaveIn();
                waveIn.DeviceNumber = 0;
                waveIn.DataAvailable += waveIn_DataAvailable;
                int sampleRate = 16000; // 16 kHz
                int channels = 1; // mono
                waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
                writer = new WaveFileWriter(audiopath.ToString(), waveIn.WaveFormat);
                waveIn.StartRecording();
            }
        }
        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (isRecording && writer != null)
            {
                writer.Write(e.Buffer, 0, e.BytesRecorded);
            }

        }

        private void disableAllOtherButtons(Button enabledButton)
        {
            btnOk.Enabled = false;
            btnCancel.Enabled = false;
            btnOpenWinSound.Enabled = false;
            btnOpenMoveSound.Enabled = false;
            btnOpenCrashSound.Enabled = false;
            btnListenWinSound.Enabled = false;
            btnListenMoveSound.Enabled = false;
            btnListenCrashSound.Enabled = false;
            btnRecordWinSound.Enabled = false;
            btnRecordMoveSound.Enabled = false;
            btnRecordCrashSound.Enabled = false;
            enabledButton.Enabled = true;
        }

        private void enableAllButtons()
        {
            btnOk.Enabled = true;
            btnCancel.Enabled = true;
            btnOpenWinSound.Enabled = true;
            btnOpenMoveSound.Enabled = true;
            btnOpenCrashSound.Enabled = true;
            btnListenWinSound.Enabled = true;
            btnListenMoveSound.Enabled = true;
            btnListenCrashSound.Enabled = true;
            btnRecordWinSound.Enabled = true;
            btnRecordMoveSound.Enabled = true;
            btnRecordCrashSound.Enabled = true;
        }
    }
}
